package com.cheng.example.atguigu.view2.client37;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;


/**
 *  阻塞队列api之抛出异常组
 */
public class BlockingQueueDemo {

    public static void main(String[] args) throws InterruptedException {
//        f1();
        String a = "123";
        String b = "123";
        String c = "123123";
        System.out.println(String.format("1 %s", a.equals(b)));
        System.out.println(String.format("2 %s", a == b));
        System.out.println(String.format("3 %s", a.intern() == b.intern()));
        System.out.println(String.format("5 %s", c == (a + b)));
        System.out.println(String.format("6 %s", c.intern() == (a + b).intern()));

        final String d = "123";
        final String e = "456";
        final String f = "123456";
        System.out.println(String.format("8 %s", f == (d + e)));

        AtomicReference<Integer> ia = new AtomicReference<>(1);
        AtomicReference<Integer> ib = new AtomicReference<>(1);
        System.out.println(ia.get() == ib.get());

        AtomicReference<Integer> ic = new AtomicReference<>(1024);
        AtomicReference<Integer> id = new AtomicReference<>(1024);

        System.out.println(ic.get() == id.get());

        Map<String,Object> map = new HashMap<>(1000, 1);

        for (int i=0;i<1000;++i){
            map.put(i+"",i);
        }
    }

    /**
     * 阻塞队列api之抛出异常组
     * add
     * remove
     * element
     */
    public static void f1(){
        BlockingQueue blockingQueue = new ArrayBlockingQueue(3);
        System.out.println(blockingQueue.add("a"));
        System.out.println(blockingQueue.add("b"));
        System.out.println(blockingQueue.add("c"));
//        System.out.println(blockingQueue.add("d"));
        System.out.println(blockingQueue.element());
        System.out.println(blockingQueue.remove());
        System.out.println(blockingQueue.remove());
        System.out.println(blockingQueue.remove());
        System.out.println(blockingQueue.remove());
        //Exception in thread "main" java.util.NoSuchElementException
//        System.out.println(blockingQueue.element());
    }

    /**
     * 阻塞队列api之返回布尔组
     */
    public static void f2(){
        BlockingQueue blockingQueue = new ArrayBlockingQueue(3);
        System.out.println(blockingQueue.offer("a"));
        System.out.println(blockingQueue.offer("b"));
        System.out.println(blockingQueue.offer("c"));

//        System.out.println(blockingQueue.offer("d", 2,TimeUnit.SECONDS));

        System.out.println(blockingQueue.peek());
        System.out.println(blockingQueue.poll());
        System.out.println(blockingQueue.poll());
        System.out.println(blockingQueue.poll());
        System.out.println(blockingQueue.peek());
    }

    /**
     * 阻塞队列api之阻塞
     */
    public static void f3() throws InterruptedException {
        BlockingQueue<String> blockingQueue = new ArrayBlockingQueue(3);
        blockingQueue.put("a");
        blockingQueue.put("b");
        blockingQueue.put("c");
        blockingQueue.put("d");

//        System.out.println(blockingQueue.offer("d", 2,TimeUnit.SECONDS));
        System.out.println(blockingQueue.take());
        System.out.println(blockingQueue.take());
        System.out.println(blockingQueue.take());
//        System.out.println(blockingQueue.take());
//        System.out.println(blockingQueue.peek());
    }

    /**
     * 阻塞队列api之阻塞
     */
    public static void f4() throws InterruptedException {
        BlockingQueue<String> blockingQueue = new ArrayBlockingQueue(3);
        System.out.println(blockingQueue.offer("a", 2,TimeUnit.SECONDS));
        System.out.println(blockingQueue.offer("b", 2,TimeUnit.SECONDS));
        System.out.println(blockingQueue.offer("c", 2,TimeUnit.SECONDS));
        System.out.println(blockingQueue.offer("d", 2,TimeUnit.SECONDS));
    }
}
